Imports System
Imports System.Configuration
Imports System.Web.Configuration

Partial Class Usercontrols_Admin_EventSettings
    Inherits UserControlBase

#Region " Enums, Memebers and Properties "



#End Region

#Region " Page Events "

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Me.MessageLabel = Me.uxMessagesLabel
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Me.Page.IsPostBack = False Then
            LoadSettings()
        End If

        Me.uxAdminPanel.Visible = ShowControls(SecurityMask.Admin, True)

    End Sub

#End Region

#Region " Save Data "

    Public Sub SaveSettings()
        Dim configuration As Configuration = WebConfigurationManager.OpenWebConfiguration("~")
        Dim AppSettingsSection As AppSettingsSection = CType(configuration.GetSection("appSettings"), AppSettingsSection)

        If Not (AppSettingsSection Is Nothing) Then

            If IsNothing(AppSettingsSection.Settings("DisplayLatestEvents")) Then
                AppSettingsSection.Settings.Add("DisplayLatestEvents", Me.uxDisplayLatestEventsCheckbox.Checked)
            Else
                AppSettingsSection.Settings("DisplayLatestEvents").Value() = Me.uxDisplayLatestEventsCheckbox.Checked
            End If

            If IsNothing(AppSettingsSection.Settings("DisplayMostViewedEvents")) Then
                AppSettingsSection.Settings.Add("DisplayMostViewedEvents", Me.uxDisplayMostViewedEventsCheckBox.Checked)
            Else
                AppSettingsSection.Settings("DisplayMostViewedEvents").Value() = Me.uxDisplayMostViewedEventsCheckBox.Checked
            End If

            If IsNothing(AppSettingsSection.Settings("DisplayMostCommentsEvents")) Then
                AppSettingsSection.Settings.Add("DisplayMostCommentsEvents", Me.uxDisplayMostCommentsEventsCheckbox.Checked)
            Else
                AppSettingsSection.Settings("DisplayMostCommentsEvents").Value() = Me.uxDisplayMostCommentsEventsCheckbox.Checked
            End If

            If IsNothing(AppSettingsSection.Settings("DisplayHighestRankedEvents")) Then
                AppSettingsSection.Settings.Add("DisplayHighestRankedEvents", Me.uxDisplayHighestRankedEventsCheckbox.Checked)
            Else
                AppSettingsSection.Settings("DisplayHighestRankedEvents").Value() = Me.uxDisplayHighestRankedEventsCheckbox.Checked
            End If

            If IsNothing(AppSettingsSection.Settings("EnableUserEvents")) Then
                AppSettingsSection.Settings.Add("EnableUserEvents", Me.uxEnableUserEventsCheckbox.Checked)
            Else
                AppSettingsSection.Settings("EnableUserEvents").Value() = Me.uxEnableUserEventsCheckbox.Checked
            End If

            configuration.Save()
        End If

    End Sub

    Public Sub SaveSettings(ByVal key As String, ByVal value As String)
        Dim configuration As Configuration = WebConfigurationManager.OpenWebConfiguration("~")
        Dim AppSettingsSection As AppSettingsSection = CType(configuration.GetSection("appSettings"), AppSettingsSection)

        If Not (AppSettingsSection Is Nothing) Then
            AppSettingsSection.Settings(key).Value = value
            configuration.Save()
        End If
    End Sub

#End Region

#Region " Load Data "

    Public Sub LoadSettings()

        If Not (AppSettingsSection Is Nothing) Then
            If IsNothing(AppSettingsSection.Settings("DisplayLatestEvents")) Then
                Me.uxDisplayLatestEventsCheckbox.Checked = True
            Else
                Me.uxDisplayLatestEventsCheckbox.Checked = AppSettingsSection.Settings("DisplayLatestEvents").Value()
            End If

            If IsNothing(AppSettingsSection.Settings("DisplayMostViewedEvents")) Then
                Me.uxDisplayMostViewedEventsCheckBox.Checked = True
            Else
                Me.uxDisplayMostViewedEventsCheckBox.Checked = AppSettingsSection.Settings("DisplayMostViewedEvents").Value()
            End If

            If IsNothing(AppSettingsSection.Settings("DisplayMostCommentsEvents")) Then
                Me.uxDisplayMostCommentsEventsCheckbox.Checked = True
            Else
                Me.uxDisplayMostCommentsEventsCheckbox.Checked = AppSettingsSection.Settings("DisplayMostCommentsEvents").Value()
            End If

            If IsNothing(AppSettingsSection.Settings("DisplayHighestRankedEvents")) Then
                Me.uxDisplayHighestRankedEventsCheckbox.Checked = True
            Else
                Me.uxDisplayHighestRankedEventsCheckbox.Checked = AppSettingsSection.Settings("DisplayHighestRankedEvents").Value()
            End If

            If IsNothing(AppSettingsSection.Settings("EnableUserEvents")) Then
                Me.uxEnableUserEventsCheckbox.Checked = False
            Else
                Me.uxEnableUserEventsCheckbox.Checked = AppSettingsSection.Settings("EnableUserEvents").Value()
            End If

        End If

    End Sub

#End Region

#Region " Form Controls Events "

#End Region

#Region " Helpers and Methods "



#End Region

End Class
